﻿using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksum;

namespace LanDBom.Models
{
    public class PlxzWj
    {
        /// <summary>
        /// 压缩文件(并保存至服务器)
        /// </summary>
        /// <param name="fileName">要压缩的所有文件（完全路径)</param>
        /// <param name="name">压缩后文件路径</param>
        /// <param name="Level">压缩级别</param>
        public static bool ZipFileMain(List<string> filenames, string name, int Level)
        {
            ZipOutputStream s = new ZipOutputStream(File.Create(name));
            Crc32 crc = new Crc32();
            //压缩级别
            s.SetLevel(Level);
            try
            {
                foreach (string file in filenames)
                {
                    //打开压缩文件
                    FileStream fs = File.OpenRead(file);
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    //建立压缩实体
                    ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(file));
                    //时间
                    entry.DateTime = DateTime.Now;
                    //空间大小
                    entry.Size = fs.Length;
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                s.Finish();
                s.Close();
            }
            return true;
        }


        /// <summary>
        /// 将多个流进行zip压缩，返回压缩后的流.
        /// </summary>
        /// <param name="streams">key：文件名；value：文件名对应的要压缩的流.</param>
        /// <returns>压缩后的流.</returns>
        public static void PackageManyZip(Dictionary<string, byte[]> fileDataDict, Dictionary<string, Stream> streams)
        {
            byte[] buffer = new byte[819200];
            MemoryStream returnStream = new MemoryStream();
            using (var zipMs = new MemoryStream())
            using (ZipOutputStream zipStream = new ZipOutputStream(zipMs))
            {
                zipStream.SetLevel(9);
                foreach (var kv in streams)
                {
                    string fileName = kv.Key;
                    using (var streamInput = kv.Value)
                    {
                        zipStream.PutNextEntry(new ZipEntry(fileName));
                        int readCount;
                        while ((readCount = streamInput.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            zipStream.Write(buffer, 0, readCount);
                        }
                        zipStream.CloseEntry(); // 关闭当前条目，不是必需的，因为 ZipOutputStream 在 Dispose 时会处理，但更清晰
                    }
                }
                zipStream.Finish();
                zipMs.Position = 0;
                zipMs.CopyTo(returnStream);
            }
            returnStream.Position = 0;
        }
    }

}